/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote;

import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Composite;

import pl.vdl.azbest.annotations.Level;
import pl.vdl.azbest.annotations.TODO;
import pl.vdl.azbest.annotations.Team;
import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.log.gui.LoggingTheGatheringGUI;
import pl.vdl.azbest.log.gui.LoggingTheGatheringUI;
import pl.vdl.azbest.mremote.com.CConnect;
import pl.vdl.azbest.mremote.gui.SWTElement;
import pl.vdl.azbest.mremote.gui.composite.CompositeComm;
import pl.vdl.azbest.mremote.gui.composite.CompositeCommSend;
import pl.vdl.azbest.mremote.gui.composite.CompositeConsole;
import pl.vdl.azbest.mremote.gui.composite.CompositeTabFolder;
import pl.vdl.azbest.mremote.gui.shell.SystemConsole;

import org.eclipse.swt.layout.GridData;

@TODO(dateAssigned = "21-03-2008", level = Level.IMPORTANT, target = "Simple develop and testing platform ", responsible = Team.AZBEST)
public class COMCommander extends SWTElement {

	// private Shell sShell = null; //
	// @jve:decl-index=0:visual-constraint="0,32"
	private Menu menuBar = null;

	private Menu submenu = null;

	private Composite composite = null;

	private Composite composite1 = null;

	private Logger logger = Logger.getLogger(getClass().getName()); // @jve:decl-index=0:

	private Composite composite2 = null;

	private Composite composite3 = null;
	{
		LoggingTheGathering.addPath(getClass().getName());
	}

	/**
	 * This method initializes sShell
	 */
	protected void createSShell() {
		sShell = new Shell();
		sShell.setText("ShellWorkspace");
		createComposite();
		// createComposite1();
		// createComposite2();
		createComposite3();
		WIN_HEIGHT = 400;
		WIN_WIDTH = 500;
		sShell.setSize(new Point(500, 400));

		sShell.setLayout(new GridLayout());
		menuBar = new Menu(sShell, SWT.BAR);
		MenuItem submenuItem = new MenuItem(menuBar, SWT.CASCADE);
		submenuItem.setText("File");
		submenu = new Menu(submenuItem);
		MenuItem push = new MenuItem(submenu, SWT.PUSH);
		push.setText("Connect");
		push
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						// System.out.println("widgetSelected()"); // TODO
						// Auto-generated
						// Event stub
						// widgetSelected()
						System.err.println("ERROR console "); // TODO
						// Auto-generated
						// Event stub
						// widgetSelected()

						logger.info("from logger info");
						CConnect.getInstance().connect();
					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
		MenuItem Disconnect = new MenuItem(submenu, SWT.PUSH);
		Disconnect.setText("Disconnect");
		MenuItem push1 = new MenuItem(submenu, SWT.PUSH);
		push1.setText("Console");
		MenuItem push2 = new MenuItem(submenu, SWT.PUSH);
		push2.setText("Exit");
		MenuItem push3 = new MenuItem(submenu, SWT.PUSH);
		push3.setText("Logging ");
		MenuItem push4 = new MenuItem(submenu, SWT.PUSH);
		push4.setText("LoggingTheGathering");
		push4
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						LoggingTheGatheringGUI ltgg = new LoggingTheGatheringGUI();
						ltgg
								.openSWTWidgetSingleton(LoggingTheGathering.isLoggerOnLoggingThegathering);
						LoggingTheGathering.isLoggerOnLoggingThegathering = true;
					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
		push3
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						LoggingTheGatheringUI LtG = new LoggingTheGatheringUI();
						LtG
								.openSWTWidgetSingleton(LoggingTheGathering.isLoggerOn);
						LoggingTheGathering.isLoggerOn = true;
					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
		push2
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						MCStop.stopMC();

					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
		push1
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						SystemConsole sc = new SystemConsole();
						sc
								.openSWTWidgetSingleton(Conf.getInstance().consoleOpen);

					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
		Disconnect
				.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						CConnect.getInstance().disconnect();
					}

					public void widgetDefaultSelected(
							org.eclipse.swt.events.SelectionEvent e) {
					}
				});
		submenuItem.setMenu(submenu);
		sShell.setMenuBar(menuBar);
		sShell.addShellListener(new org.eclipse.swt.events.ShellAdapter() {
			public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
				MCStop.stopMC();

			}
		});
		sShell.addDisposeListener(new org.eclipse.swt.events.DisposeListener() {
			public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
				// System.out.println("Shell closed");

			}
		});
		sShell.addShellListener(new org.eclipse.swt.events.ShellAdapter() {
			public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
				MCStop.stopMC();
			}
		});

		init();
	}

	/**
	 * This method initializes composite
	 * 
	 */
	private void createComposite() {
		composite = new CompositeComm(this.sShell, SWT.NONE);

	}

	/**
	 * This method initializes composite1
	 * 
	 */
	private void createComposite1() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		composite1 = new CompositeConsole(sShell, SWT.NONE);
		composite1.setLayout(new GridLayout());
		composite1.setLayoutData(gridData);
	}

	/**
	 * This method initializes composite2
	 * 
	 */
	private void createComposite2() {
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalAlignment = GridData.CENTER;
		composite2 = new CompositeCommSend(sShell, SWT.NONE);
		composite2.setLayout(new GridLayout());
		composite2.setLayoutData(gridData1);
	}

	private void createComposite3() {

		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;
		composite3 = new CompositeTabFolder(sShell, SWT.NONE);
		composite3.setLayout(new GridLayout());
		composite3.setLayoutData(gridData1);
	}

	private void init() {
		setSWTWidgetPositionCenetr(0, 0);
		lockSWTWidgetSize();

	}

}
